package com.briup.run.web.servlet;

import com.briup.run.bean.Memberinfo;
import com.briup.run.service.MemberInfoService;
import com.briup.run.service.impl.MemberInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Auther: vanse(lc))
 * @Date: 2022/10/10-10-10-11:05
 * @Description：注销
 */
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 清除session
        HttpSession session = request.getSession();
        Memberinfo memberinfo = (Memberinfo) session.getAttribute("memberinfo");
        session.invalidate();
        // 改为离线
        MemberInfoService memberInfoService = new MemberInfoServiceImpl();
        memberInfoService.logout(memberinfo.getId());
        // 重定向登录页面
        response.sendRedirect(request.getContextPath() + "/login.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

